﻿using System;
using System.Collections.Generic;
using System.Drawing;


namespace WindowsFormsApp1.Common
{
    public static class FileDll
    {

        /// <summary>
        /// 区域找图,只适用于子图片与父图片对比,不可适应发生拉伸,或者背景颜色变化的情况
        /// </summary>
        /// <param name="rect">大图的区域</param>
        /// <param name="serchBmp">小图</param>
        /// <param name="h">相似度</param>
        /// <returns></returns>
        public static Point FindPic(Bitmap prentBmp, Bitmap serchBmp, int h)
        {
            Point point = new Point(-1, -1);
            //大图采样点集合,用小图的第一个点颜色来限制大图的存在
            List<ImgPoint> parentPoints = GetImgPoints(prentBmp, serchBmp);
            //小图采样点集合
            List<ImgPoint> CildPoints = GetImgPoints(serchBmp);

            //从大图一个点一个点开始排查
            for (int i = 0; i < parentPoints.Count; i++)
            {
                if (prentBmp.Width - parentPoints[i].Point.X < serchBmp.Width)
                {
                    continue;
                }
                if (prentBmp.Height - parentPoints[i].Point.Y < serchBmp.Height)
                {
                    continue;
                }
                int count = ComparePoint(parentPoints[i], prentBmp, serchBmp, h);
                if (h <= count)
                {
                    point.X = parentPoints[i].Point.X + (serchBmp.Width / 2);
                    point.Y = parentPoints[i].Point.Y + (serchBmp.Height / 2);
                    serchBmp.Dispose();
                    prentBmp.Dispose();
                    return point;
                }

            }
            serchBmp.Dispose();
            prentBmp.Dispose();
            return point;
        }
        #region 方法实现
        /// <summary>
        /// 随机生成采样点
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="h">采样比率，范围为0-1，决定图片像素的采样百分比，大于等于1为采样点个数</param>
        /// <returns></returns>
        public static List<ImgPoint> GetImgPoints(Bitmap img)
        {
            int width_max = img.Width;
            int height_max = img.Height;
            int count = width_max * height_max;
            List<ImgPoint> points = new List<ImgPoint>();//采样点的集合
            for (int i = 0; i < width_max; i += 5)
            {
                for (int j = 0; j < height_max; j++)
                {
                    Point point = new Point();
                    point.X = i;
                    point.Y = j;
                    Color imgColor = img.GetPixel(i, j);
                    ImgPoint imgPoint = new ImgPoint(point, imgColor);
                    points.Add(imgPoint);
                }
            }
            return points;
        }

        public static List<ImgPoint> GetImgPoints(Bitmap img, Bitmap searchImg)
        {
            int width_max = img.Width;
            int height_max = img.Height;
            int count = width_max * height_max;
            List<ImgPoint> points = new List<ImgPoint>();//采样点的集合
            for (int i = 0; i < width_max; i += 5)
            {
                for (int j = 0; j < height_max; j++)
                {

                    Point point = new Point();
                    point.X = i;
                    point.Y = j;
                    Color imgColor = img.GetPixel(i, j);
                    if (searchImg.GetPixel(0, 0).EqualsRGB(imgColor))
                    {
                        points.Add(new ImgPoint(point, imgColor));
                    }

                }
            }
            return points;
        }

        /// <summary>
        /// 扩展函数，比较图片的 RGB 是否相同
        /// </summary>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <returns></returns>
        public static bool EqualsRGB(this Color color1, Color color2)
        {
            int h = 10;
            int cr = color1.R - color2.R;
            int cb = color1.B - color2.B;
            int cg = color1.G - color2.G;
            if (h > cr && cr > -h && h > cb && cb > -h && h > cg && cg > -h)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 以大图某个点开始,计算这点与小图的相似度
        /// </summary>
        /// <param name="imgPoint">大图相对的点</param>
        /// <param name="prentBmp">大图</param>
        /// <param name="serchBmp">小图</param>
        /// <param name="h">相似度</param>
        /// <returns></returns>
        public static int ComparePoint(ImgPoint imgPoint, Bitmap prentBmp, Bitmap serchBmp, int h)
        {
            int count = 0;
            int width = serchBmp.Width;
            int height = serchBmp.Height;
            int num = 101 - h;
            for (int i = 0; i < width; i += num)
            {
                for (int j = 0; j < height; j += num)
                {
                    if (serchBmp.GetPixel(i, j).EqualsRGB(prentBmp.GetPixel((i + imgPoint.Point.X), (j + imgPoint.Point.Y))))
                    {
                        count++;
                    }
                }
            }
            return Convert.ToInt32(num * num * count * 100 / (width * height));
        }

        /// <summary>
        /// 以大图某个点开始,计算这点与小图的相似度(数组处理话费时间太大,暂无法处理1)
        /// </summary>
        /// <param name="imgPoint">大图的相对点</param>
        /// <param name="prentBmpList">大图所有的点</param>
        /// <param name="serchBmpList">小图所有的点</param>
        /// <param name="serchBmp">小图对象</param>
        /// <param name="h">比例</param>
        /// <returns></returns>
        public static int ComparePoint(ImgPoint imgPoint, List<ImgPoint> prentBmpList, List<ImgPoint> serchBmpList, Bitmap serchBmp, int h)
        {
            int count = 0;
            prentBmpList = prentBmpList.FindAll(f => f.Point.X >= imgPoint.Point.X && f.Point.X <= imgPoint.Point.X + serchBmp.Width && f.Point.Y >= imgPoint.Point.Y && f.Point.Y <= imgPoint.Point.Y + serchBmp.Height);
            for (int i = 0; i < serchBmpList.Count; i++)
            {
                ImgPoint childPoint = serchBmpList[i];
                ImgPoint parentPoint = prentBmpList.Find(f => f.Point.X == childPoint.Point.X + imgPoint.Point.X && f.Point.Y == childPoint.Point.Y + imgPoint.Point.Y);
                if (parentPoint == null)
                {
                    continue;
                }
                if (childPoint.Color.EqualsRGB(parentPoint.Color))
                {
                    if (i > 2000)
                    {
                        if (count * 100 < 1000 * h)
                        {
                            return 0;
                        }
                    }
                    count++;
                }
            }
            var a = serchBmpList.Count;
            var b = count;
            if (imgPoint.Point.X > 180 && imgPoint.Point.Y > 100)
            {
                var c = 1;
                var d = Convert.ToInt32(count * 100 / (serchBmpList.Count));
            }
            return Convert.ToInt32(count * 100 / serchBmpList.Count);

        }


        #endregion


    }
}
